﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AttributesFramework
{
    internal abstract class Watcher
    {
        private static Dictionary<int, List<string>> _DirtyObjects = new Dictionary<int, List<string>>();
        private static Dictionary<int, List<string>> _AcessedObjects = new Dictionary<int, List<string>>();

        public static bool isObjectDirty(object p_Object)
        {
            return _DirtyObjects.ContainsKey(p_Object.GetHashCode()) && _DirtyObjects[p_Object.GetHashCode()].Count > 0;
        }

        public static bool isObjectAccessed(object p_Object)
        {
            return _AcessedObjects.ContainsKey(p_Object.GetHashCode()) && _AcessedObjects[p_Object.GetHashCode()].Count > 0;
        }

        public static void setObjectClean(object p_Object)
        {
            if(_DirtyObjects.ContainsKey(p_Object.GetHashCode()))
                _DirtyObjects.Remove(p_Object.GetHashCode());
        }

        public static void setObjectUnacessed(object p_Object)
        {
            if (_AcessedObjects.ContainsKey(p_Object.GetHashCode()))
                _AcessedObjects.Remove(p_Object.GetHashCode());
        }

        public static bool isPropertyDirty(object p_Object, string p_PropertyName)
        {
            return _DirtyObjects.ContainsKey(p_Object.GetHashCode()) && _DirtyObjects[p_Object.GetHashCode()].Contains(p_PropertyName);
        }

        public static bool isPropertyAcessed(object p_Object, string p_PropertyName)
        {
            return _AcessedObjects.ContainsKey(p_Object.GetHashCode()) && _AcessedObjects[p_Object.GetHashCode()].Contains(p_PropertyName);
        }

        public static void setPropertyDirty(object p_Object, string p_PropertyName)
        {
            if (!_DirtyObjects.ContainsKey(p_Object.GetHashCode()))
                _DirtyObjects[p_Object.GetHashCode()] = new List<string>();

            if(!_DirtyObjects[p_Object.GetHashCode()].Contains(p_PropertyName))
                _DirtyObjects[p_Object.GetHashCode()].Add(p_PropertyName);
        }

        public static void setPropertyAccessed(object p_Object, string p_PropertyName)
        {
            if (!_AcessedObjects.ContainsKey(p_Object.GetHashCode()))
                _AcessedObjects[p_Object.GetHashCode()] = new List<string>();

            if (!_AcessedObjects[p_Object.GetHashCode()].Contains(p_PropertyName))
                _AcessedObjects[p_Object.GetHashCode()].Add(p_PropertyName);
        }

        public static void setPropertyClean(object p_Object, string p_PropertyName)
        {
            if (!_DirtyObjects.ContainsKey(p_Object.GetHashCode()))
                return;

            if (_DirtyObjects[p_Object.GetHashCode()].Contains(p_PropertyName))
                _DirtyObjects[p_Object.GetHashCode()].Remove(p_PropertyName);
        }

        public static void setPropertyUnaccessed(object p_Object, string p_PropertyName)
        {
            if (!_AcessedObjects.ContainsKey(p_Object.GetHashCode()))
                return;

            if (_AcessedObjects[p_Object.GetHashCode()].Contains(p_PropertyName))
                _AcessedObjects[p_Object.GetHashCode()].Remove(p_PropertyName);
        }

    }
}
